package com.loren.tetris.windows.ui;

import com.loren.tetris.data.GameData;
import com.loren.tetris.data.constant.DataConstant;
import com.loren.tetris.utils.AssistiveUtils;

import java.awt.*;

public abstract class Box {

    /**
     * 游戏边框的X坐标
     */
    private final int boxX;

    /**
     * 游戏边框的Y坐标
     */
    private final int boxY;

    /**
     * 游戏边框的宽度
     */
    private final int boxW;

    /**
     * 游戏边框的高度
     */
    private final int boxH;

    /**
     * 游戏数据
     */
    private final GameData gameData;

    public Box(int boxX, int boxY, int boxW, int boxH, GameData gameData) {
        this.boxX = boxX;
        this.boxY = boxY;
        this.boxW = boxW;
        this.boxH = boxH;
        this.gameData = gameData;
    }

    public void paint(Graphics g) {
        g.setColor(DataConstant.COLOR_BOX_BORDER);
        g.drawRect(this.boxX, this.boxY, this.boxW, this.boxH);
        this.gameAction(g);
    }

    /**
     * 游戏边框内的行为
     */
    protected abstract void gameAction(Graphics g);

    public int getBoxX() {
        return boxX;
    }

    public int getBoxY() {
        return boxY;
    }

    public int getBoxW() {
        return boxW;
    }

    public int getBoxH() {
        return boxH;
    }

    public GameData getGameData() {
        return gameData;
    }

    /**
     * 绘制游戏边框的标题
     */
    protected void drawBoxTitle(Graphics2D g, String title) {
        // 消除绘制字体时出现的锯齿
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setFont(new Font(AssistiveUtils.getFontFamily(), Font.BOLD, 22));
        g.setColor(DataConstant.COLOR_BOX_TITLE);
        g.drawString(title, this.getBoxX() + 10, this.getBoxY() + 28);
    }
}
